package com.alm.token.ui.viewpagefragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.QuantitativeOrderDetailActivity;
import com.alm.token.ui.activity.QuantitativeOrderOpenAddActivity;
import com.alm.token.ui.activity.QuantitativeSplashActivity;
import com.alm.token.ui.activity.StakingOrderIndexActivity;
import com.alm.token.ui.activity.StakingSplashActivity;
import com.alm.token.ui.activity.TransformActivity;
import com.alm.token.ui.activity.VipApplyActivity;
import com.alm.token.ui.data.bean.FortunePageData;
import com.alm.token.ui.data.bean.LH20orderHistoryData;
import com.alm.token.ui.data.bean.UserListLHData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.FortunePageView;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.widget.bannerview.BGABanner;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.haozhang.lib.SlantedTextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class FortunePageFrag extends BaseViewPageFragment implements FortunePageView<FortunePageData> {


    Unbinder unbinder;
    @BindView(R.id.lt_transform_rightnow_body)
    LinearLayout ltTransformRightnowBody;
    @BindView(R.id.rly_item1)
    LinearLayout rlyItem1;
    @BindView(R.id.rly_item2)
    LinearLayout rlyItem2;
    @BindView(R.id.tv_today_totalmoney)
    TextView tvTodayTotalmoney;
    @BindView(R.id.tv_joinvip)
    TextView tvJoinvip;
    @BindView(R.id.tv_lianghua)
    TextView tvLianghua;
    @BindView(R.id.tv_wakuang)
    TextView tvWakuang;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;

    private RefreshLoadMoreManager<LH20orderHistoryData.DataBean> manager;
    private List<LH20orderHistoryData.DataBean> dataBeans = new ArrayList<>();


    private int ActivityForResultCode = 1;

    public static FortunePageFrag newInstance() {
        FortunePageFrag fragment = new FortunePageFrag();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.viewpage_fragment_fortune;
    }

    @Override
    protected void initVariables() {


//        imagesBanner.add("");
    }

    @Override
    protected void initViews(View view) {
        initManager();
    }

    private void initManager() {
        OrderAdapter adapter = new OrderAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setOnListItemClickListener(new RefreshLoadMoreManager.OnListItemClickListener() {
                    @Override
                    public void onListItemClick(BaseAdapter.BaseItem item, int position) {

                        if (item instanceof LH20orderHistoryData.DataBean) {
                            LH20orderHistoryData.DataBean dataBean = (LH20orderHistoryData.DataBean) item;
                            if (dataBean.getTubiaoName().equalsIgnoreCase("Staking")) {
                                Intent intent = new Intent(getActivity(), StakingOrderIndexActivity.class);
                                intent.putExtra("moneyType", 0);
                                intent.putExtra("orderSn", dataBean.getOrdersn());
                                startActivityForResult(intent, ActivityForResultCode);
                            } else {
                                startActivityForResult(QuantitativeSplashActivity.class, ActivityForResultCode);
                            }
                        }


//                        onViewClicked(rlyItem2);
                    }
                })
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .build();
        recyclerView.setNestedScrollingEnabled(false);

    }

    @Override
    protected void loadData() {
        preenter.getLH2OrderHomeData();
//        preenter.  getLH2OorderHistoryData("1","100");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.module1:
                break;

        }
    }

    @OnClick(R.id.tv_clickreload)
    public void onViewClicked() {
        loadData();
    }

    @Override
    public void getData(FortunePageData fortunePageData) {

        if (fortunePageData.getStatus() == 0) {
            if (EmptyUtils.isNotEmpty(fortunePageData.getData())) {
                FortunePageData.DataBean data = fortunePageData.getData();
                tvTodayTotalmoney.setText("$" + CommonUtil.saveToPoint2WithNo(data.getMoney()));
                tvLianghua.setText(CommonUtil.saveToPoint4WithNo(data.getLianghua()));
                tvWakuang.setText(CommonUtil.saveToPoint4WithNo(data.getWakuang()));
                dataBeans.clear();
                if (EmptyUtils.isNotEmpty(data.getLlist())) {
                    for (FortunePageData.DataBean.ListBean listBean : data.getLlist()) {
                        /**
                         * ordersn : ETH-20190814-m12082105434
                         * purl : /static/images/lianghua.png
                         * wname : AlphaGo
                         * wlevel : 2
                         * wday : 0
                         * wdate : 1565751274000
                         * orderid : 1899
                         * status : 0
                         */
                        LH20orderHistoryData.DataBean dataBean = new LH20orderHistoryData.DataBean();
                        dataBean.setTubiao(listBean.getPurl());
                        dataBean.setDays(listBean.getWday());
                        dataBean.setOrderLevel(listBean.getWlevel());
                        dataBean.setOrdersn(listBean.getOrdersn());
                        dataBean.setStartDate(listBean.getWdate());
                        dataBean.setStatus(listBean.getStatus());
                        dataBean.setTubiaoName(listBean.getWname());
                        dataBean.setLayoutId(OrderAdapter.LAYOUT_ID);
                        dataBean.setType(OrderAdapter.LAYOUT_TYPEAGO);
                        dataBeans.add(dataBean);
                    }
                }
                if (EmptyUtils.isNotEmpty(data.getWlist())) {
                    for (FortunePageData.DataBean.ListBean listBean : data.getWlist()) {
                        LH20orderHistoryData.DataBean dataBean = new LH20orderHistoryData.DataBean();
                        dataBean.setTubiao(listBean.getPurl());
                        dataBean.setDays(listBean.getWday());
                        dataBean.setOrderLevel(listBean.getWlevel());
                        dataBean.setOrdersn(listBean.getOrdersn());
                        dataBean.setStartDate(listBean.getWdate());
                        dataBean.setStatus(listBean.getStatus());
                        dataBean.setTubiaoName(listBean.getWname());
                        dataBean.setLayoutId(OrderAdapter.LAYOUT_ID);
                        dataBean.setType(OrderAdapter.LAYOUT_TYPESTK);
                        dataBeans.add(dataBean);
                    }
                }
                manager.setData(dataBeans);
            }
            if (dataBeans.size() == 0) {
                swipeRefreshLayout.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
            } else {
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);
            }

        } else {
            showToast(fortunePageData.getMsg());
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast("需要重新登录!");
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.lt_transform_rightnow_body, R.id.rly_item1, R.id.rly_item2, R.id.tv_joinvip})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lt_transform_rightnow_body:
                startActivity(TransformActivity.class);
                break;
//            case R.id.tv_myfortune:
////                showToast("click my fortune!");
//                startActivity(MyFortuneActivity.class);
//                break;
            case R.id.rly_item1:
//                showToast(getString(R.string.currency_hint1_warn));
                startActivityForResult(StakingSplashActivity.class, ActivityForResultCode);
                break;
            case R.id.rly_item2:
                startActivityForResult(QuantitativeSplashActivity.class, ActivityForResultCode);
                break;
            case R.id.tv_joinvip:
                startActivity(VipApplyActivity.class);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ActivityForResultCode) {
            if (resultCode == QuantitativeOrderOpenAddActivity.AddAppendSucc ||
                    resultCode == QuantitativeOrderDetailActivity.AppendSucc) {
//                showToast("添加成功，刷新列表");
                loadData();
            }
        }
    }


    public class OrderAdapter extends BaseAdapter<LH20orderHistoryData.DataBean> {

        public static final int LAYOUT_ID = R.layout.item_lianghua20_order;
        public static final int LAYOUT_TYPESTK = 1;
        public static final int LAYOUT_TYPEAGO = 2;

        public OrderAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, LH20orderHistoryData.DataBean item, int itemType) {

            ImageView imageView = (ImageView) helper.getView(R.id.img_order_tubiao);
            GlideApp.with(context).load(ConStant.HOST + item.getTubiao())
                    .error(R.drawable.icon_arctic).into(imageView);
            helper.setText(R.id.tv_tubiaoName, item.getTubiaoName());
            helper.setText(R.id.tv_orderLevel, (item.getType() == LAYOUT_TYPESTK ? "STA L" : "AGO L") + item.getOrderLevel());
            helper.setText(R.id.tv_tianshu, item.getDays() + getString(R.string.lh20_hint_day));
            helper.setText(R.id.tv_ordernumber, item.getOrdersn());
            helper.setText(R.id.tv_addDate, CommonUtil.getDateTimeFromMillisecond3(item.getStartDate()));

            helper.setText(R.id.tv_orderstaue, item.getType() == LAYOUT_TYPESTK ?
                    getString(item.getStatus() == 1 ? R.string.staking_hint_staking_stauewaiting :
                            item.getStatus() == 2 ? R.string.tv_hint_activity_order_staue_paywait :
                                    item.getStatus() == 3 ? R.string.staking_hint_staking_stauedoing
                                            : R.string.staking_hint_staking_nowgo) : "");
        }

    }
}
